from django.shortcuts import render, redirect, HttpResponse
from .models import Test, UserInfo


# Create your views here.
def index(request):
    return render(request, 'index.html')


def login(request):
    # print(request)
    # for i in request:
    #     print(i)
    if request.method == 'POST':
        # print(request.POST)
        # for i in request.POST:
        #     print(i)
        # print(request.POST.get('email'))
        # print(request.POST.get('password'))
        # print(request.POST.get('rememberMe'))
        # get方法能够从数组或字典中得到第一个数据
        email = request.POST.get('email')
        password = request.POST.get('password')
        rememberMe = request.POST.get('rememberMe')
        if email == 'lxgzhw@lxgzhw.com' and password == 'lxgzhw':
            return redirect('index')
        else:
            return HttpResponse('用户名或密码错误')
    return render(request, 'login.html')


def user_list(request):
    # 去数据库查询所有的数据
    # 用ORM这个工具去查询数据库,不用自己去查询
    users = UserInfo.objects.all()
    return render(request, 'user_list.html', locals())


def user_add(request):
    if request.method == 'POST':
        # 添加用户
        new_user_name = request.POST.get('username')
        # new_user = UserInfo(name=new_user_name)
        # new_user.save()
        res = UserInfo.objects.create(name=new_user_name)
        if res:
            print(res)
            return redirect('user_list')
    return render(request, 'add_user.html')
